package com.pijunn.payment



import grails.test.mixin.*

import org.junit.*

import com.fedex.rate.RateService
import com.pijunn.common.Dimensions
import com.pijunn.common.MailingAddress
import com.pijunn.common.enums.ServiceType
import com.pijunn.report.Contact
import com.pijunn.shipping.ShippingRate
import com.pijunn.webservice.fedex.ConfigurableStreamHandlerFactory

/**
 * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
 */
@TestFor(PaymentService)
@Mock([ShippingRate,Contact])
class PaymentServiceTests {

    void testShippingSpeeds() {
		def paymentService = new PaymentService()
		def speeds = paymentService.getShippingSpeeds()
        assertTrue 'Shipping Speed size', speeds.size() > 0
    }

    void testShippingCarriers() {
		def paymentService = new PaymentService()
		def carriers = paymentService.getShippingCarriers()
        assertEquals 'Carrier size', carriers.size(), 1
    }
	
	void testShippingContainerTypes() {
		def shippingList = []
			shippingList << new ShippingRate(packageType:'FedEx Envelope', dimensions:new Dimensions(height:12, width:9.5, depth:1),emptyWeight:0.25).save([flush:true])
			//shippingList << new ShippingRate(packageType:'FedEx Pak/Box', dimensions:new Dimensions(height:19.5, width:15, depth:3.5),emptyWeight:1.1).save()
			shippingList << new ShippingRate(packageType:'FedEx Pak/Box', dimensions:new Dimensions(height:19.5, width:15, depth:3.5),emptyWeight:1.2).save()
			
		def paymentService = new PaymentService()
		def containers = paymentService.getShippingContainerTypes()
		assertEquals 'Container size', containers.size(), 2

	}
	
	void testHandlingCharge() {
		def paymentService = new PaymentService()
		def charge = paymentService.getHandlingCharge()
		assertEquals 'incorrect Handling Charge', charge, 5.50
	}
	
	void testShippingCharge() {

		def from = new MailingAddress(address1: '1316 Durango Ridge Rd', address2: '', city: 'Durango', state: 'CO', zipCode: '81301', country: 'US')
		//def to = new Contact(name: 'Joe', email: 'b@abc.com', malingAddress: new MailingAddress(address1: '12512 Caminito Mira Del Mar', address2: 'suite 123', city: 'San Diego', state: 'CA', zipCode: '92130', country: 'US')).save(failOnError: true)
		def to = new MailingAddress(address1: '12512 Caminito Mira Del Mar', address2: 'suite 123', city: 'San Diego', state: 'CA', zipCode: '92130', country: 'US')
		
		//assertEquals 'Incorrect Address', fromAddr.address1, '1316 Durango Ridge Rd'
		
		def shippingList = []
		shippingList << new ShippingRate(packageType:'FedEx Envelope', dimensions:new Dimensions(height:12, width:9.5, depth:1),emptyWeight:0.25).save([flush:true])
		shippingList << new ShippingRate(packageType:'FedEx Pak/Box', dimensions:new Dimensions(height:19.5, width:15, depth:3.5),emptyWeight:1.1).save()

		def paymentService = new PaymentService()
		def mockConfig = new ConfigObject()
		// Set options on mockConfig
		mockConfig.service.fedex.RateWebServiceClient.key="f5IX8dxbAMxfxYmA"
		mockConfig.service.fedex.RateWebServiceClient.password="XciphdFMohHEEzOSDPy56nHe3"
		mockConfig.service.fedex.RateWebServiceClient.accountNumber="510087780"
		mockConfig.service.fedex.RateWebServiceClient.meterNumber="100148522"
		paymentService.grailsApplication = new Expando(config: mockConfig)
		// Initialize the service
		URL.setURLStreamHandlerFactory(new ConfigurableStreamHandlerFactory("classpath", new com.pijunn.protocols.classpath.Handler(ClassLoader.getSystemClassLoader())));
		URL url = new URL("classpath:com/fedex/rate/RateService_v13.wsdl");
		RateService service = new RateService(url);
		paymentService.rateClient = service.getRateServicePort();

		def details = paymentService.getShippingCharge(from, to, shippingList[0], ServiceType.FEDEX_2_DAY, new BigDecimal('100.00'))
	
		assertTrue 'details returned', details.size() > 0	
	}
}
